package com.unitechs.trap.common.pojo.trap.entity;

import com.unitechs.trap.common.pojo.config.ExConfiguration;
import lombok.Data;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import java.io.Serializable;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
public class VarData implements Serializable {
    private String trapOid;
    private String varOid;
    private String varName;
    private String varDesc;
    private String varValue;

    public static int getVarOidCode(String version){
        int cell= ExConfiguration.getExConfigurationMap(version).get(version+"_VAR_OID_CELL");
        return cell;
    }



    private int getCellIndex(String version, String cellKey) {
        return ExConfiguration.getExConfigurationMap(version).get(version + "_" + cellKey);
    }

    public void setTrapOid(VarData lastData, HSSFSheet sheet, int row, String version) {
        int cellIndex = getCellIndex(version, "TRAP_OID_CELL");
        Optional<String> cellValue = getCellValue(sheet.getRow(row), cellIndex);
        this.trapOid = cellValue.orElse(lastData.getTrapOid());
    }

    public void setVarOid(HSSFSheet sheet, int row, String version) {
        int cellIndex = getCellIndex(version, "VAR_OID_CELL");
        this.varOid = sheet.getRow(row).getCell(cellIndex).toString();
    }

    public void setVarName(HSSFSheet sheet, int row, String version) {
        int cellIndex = getCellIndex(version, "VAR_NAME_CELL");
        this.varName = sheet.getRow(row).getCell(cellIndex).toString();
    }

    public void setVarDesc(VarData varData, VarData lastData, HSSFSheet sheet, int row, String version) {
        int cellIndex = getCellIndex(version, "VAR_TITLE_CELL");
        Optional<String> cellValue = getCellValue(sheet.getRow(row), cellIndex);
        if (row == 1 || varData.getTrapOid().equals(lastData.getTrapOid())) {
            this.varDesc = cellValue.orElse(lastData.getVarDesc());
        } else {
            this.varDesc = cellValue.orElse("");
        }
    }

    private Optional<String> getCellValue(HSSFRow row, int cellIndex) {
        return Optional.ofNullable(row)
                .map(r -> r.getCell(cellIndex))
                .map(Object::toString)
                .filter(s -> !s.isEmpty());
    }


    public void setVarValue(VarData varData, VarData lastData, HSSFSheet sheet, int row, String version) {
        int cell = ExConfiguration.getExConfigurationMap(version).get(version + "_VAR_CON_CELL");

        Optional<String> cellValue = getCellValue(sheet.getRow(row), cell);
        if (row != 1 && (!cellValue.isPresent() || cellValue.get().trim().isEmpty()) && !lastData.getTrapOid().equals(varData.getTrapOid())) {
            this.varValue = "";
        } else if (row != 1 && (!cellValue.isPresent() || cellValue.get().trim().isEmpty()) && lastData.getTrapOid().equals(varData.getTrapOid())) {
            this.varValue = lastData.getVarValue();
        } else {
            String str = cellValue.get().trim().toLowerCase();

            String regexAlarm = "@alarmcode,=,[0-9]+";
            String regexNum = "[0-9]+";
            String groupALL = "";
            String groupNum = "";
            Pattern pattern = Pattern.compile(regexAlarm);
            Matcher matcher = pattern.matcher(str);
            while (matcher.find()) {
                groupALL = matcher.group(0);
            }

            pattern = Pattern.compile(regexNum);
            matcher = pattern.matcher(groupALL);
            while (matcher.find()) {
                groupNum = matcher.group(0);
            }
            this.varValue = groupNum;
        }
        if (varData.getVarValue() == null || varData.getVarValue().isEmpty()) {
            varData.setVarValue(null);
        }
    }

}
